# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_compiler=clang

_realname=mlir
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
_pkgver=20.1.5
pkgver=${_pkgver/-/}
pkgrel=1
pkgdesc="Multi-Level IR Compiler Framework for LLVM (mingw-w64)"
url="https://mlir.llvm.org/"
msys2_references=(
  "cpe: cpe:/a:llvm:llvm"
  "aur: mlir"
  "gentoo: "
)
license=("spdx:Apache-2.0 WITH LLVM-exception")
arch=(any)
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
depends=("${MINGW_PACKAGE_PREFIX}-llvm-libs")
makedepends=($([[ ${_compiler} != clang ]] && echo \
               "${MINGW_PACKAGE_PREFIX}-cc" || echo \
               "${MINGW_PACKAGE_PREFIX}-clang" \
               "${MINGW_PACKAGE_PREFIX}-lld")
             "${MINGW_PACKAGE_PREFIX}-llvm"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
            #"${MINGW_PACKAGE_PREFIX}-python"
            #"${MINGW_PACKAGE_PREFIX}-python-numpy"
            #"${MINGW_PACKAGE_PREFIX}-pybind11")
# optdepends=("${MINGW_PACKAGE_PREFIX}-python: Python bindings")
options=('!strip')
_pkgfn=${_realname}-${_pkgver}.src
_url=https://github.com/llvm/llvm-project/releases/download/llvmorg-${_pkgver}
source=("${_url}/${_pkgfn}.tar.xz"{,.sig}
        "${_url}/cmake-${_pkgver}.src.tar.xz"{,.sig}
        0001-tablegen.patch
        0002-add-missing-compile-definitions.patch
        0003-Fix-build-MLIRExecutionEngineShared.patch
        0004-disable-mlir-pdll-lsp-server-with-clang.patch)
sha256sums=('657a49573496edb7f53d1899ef65546db916ba6f738fd51c119de8952877108f'
            'SKIP'
            '1b5abaa2686c6c0e1f394113d0b2e026ff3cb9e11b6a2294c4f3883f1b02c89c'
            'SKIP'
            '3f61c66d5615e387ac1c00dbeb1b5840305ff578de81d60eb65553ff17ced8c6'
            'd007b26d4c97667518f524105d5a7a14c9fe9e5f595fd48562dacb722ea650fa'
            'f10e69fdc3d2045118fde247ebaee1f3390166e117809d7bf3c543c95743b6cf'
            'f0b957f839577c7da4fc112e626452aac7ee2b28c78ae7f4b25d8066121330e4')
validpgpkeys=('B6C8F98282B944E3B0D5C2530FC3042E345AD05D'  # Hans Wennborg, Google.
              '474E22316ABF4785A88C6E8EA2C794A986419D8A'  # Tom Stellard
              'D574BD5D1D0E98895E3BF90044F2485E45D59042') # Tobias Hieta

prepare() {
  [[ -d ./cmake ]] && rm -rf ./cmake
  mv cmake-${_pkgver}.src cmake

  cd "${srcdir}"/${_pkgfn}
  patch -p1 -i "${srcdir}"/0001-tablegen.patch
  # For some reason the COMPILE_DEFINITIONS didn't propgate to source files
  # from the library, Either a bug from CMake or LLVM.
  patch -p1 -i "${srcdir}"/0002-add-missing-compile-definitions.patch
  patch -p1 -i "${srcdir}"/0003-Fix-build-MLIRExecutionEngineShared.patch

  if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]]; then
    patch -p1 -i "${srcdir}"/0004-disable-mlir-pdll-lsp-server-with-clang.patch
  fi
}

build() {
  mkdir -p build-${MSYSTEM} && cd build-${MSYSTEM}

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  if [ "${_compiler}" == "clang" ]; then
    export CC='clang'
    export CXX='clang++'
    _extra_config+=(-DLLVM_ENABLE_LLD=ON)
  fi

  CXXFLAGS+=" -Wno-dangling-assignment-gsl" \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -GNinja \
    "${_extra_config[@]}" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_SYSTEM_IGNORE_PATH=/usr/lib \
    -DLLVM_HOST_TRIPLE="${CARCH}-w64-windows-gnu" \
    -DPython3_EXECUTABLE=${MINGW_PREFIX}/bin/python.exe \
    -DLLVM_LINK_LLVM_DYLIB=ON \
    -DLLVM_BUILD_LLVM_DYLIB=ON \
    -DMLIR_BUILD_MLIR_C_DYLIB=ON \
    -DMLIR_LINK_MLIR_DYLIB=OFF \
    -DLLVM_BUILD_TOOLS=ON \
    -DLLVM_BUILD_UTILS=ON \
    -DLLVM_ENABLE_PIC=ON \
    -DMLIR_INCLUDE_TESTS=OFF \
    -DCMAKE_C_VISIBILITY_PRESET=hidden \
    -DCMAKE_CXX_VISIBILITY_PRESET=hidden \
    -DMLIR_ENABLE_BINDINGS_PYTHON=OFF \
    -Wno-dev \
    ../$_pkgfn

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install build-${MSYSTEM}

  install -Dm644 "${srcdir}"/${_pkgfn}/LICENSE.TXT "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
